package rj.cometome;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.util.Log;

public class CtMRenderer implements GLSurfaceView.Renderer {

	CtMView m_glView;

	public float xAngle = 0;
	public float yAngle = 0;
	public float zAngle = 0;
	
	public float xTrans = 0;
	public float yTrans = 0;
	public float zTrans = 0;
	
	float[] m_fProjMatrix = new float[16];
	float[] m_fViewMatrix = new float[16];
	float[] m_fMVPMatrix = new float[16];
    
	Arrow m_arrow;
	
    public CtMRenderer(CtMView view) {
    	m_glView = view;
    }
    
    float angle = 0;
	
    @Override
    public void onDrawFrame(GL10 arg0) {
    	GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    	
    	Matrix.setLookAtM(m_fViewMatrix, 0, 0, 0, -3, xTrans, yTrans, zTrans, 0, 1, 0);
    	
    	Matrix.multiplyMM(m_fMVPMatrix, 0, m_fProjMatrix, 0, m_fViewMatrix, 0);
  
    	m_arrow.draw(m_fMVPMatrix);
    }
    
    @Override
    public void onSurfaceChanged(GL10 arg0, int width, int height) {
    	if (height == 0) height = 1;
    	
    	GLES20.glViewport(0, 0, width, height);
    	
    	float ratio = (float)width/height;
    	
    	Matrix.frustumM(m_fProjMatrix, 0, -ratio, ratio, -1, 1, 1, 10);
    }
    
    @Override
    public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
    	GLES20.glClearColor(0, 0, 0, 1);
    	GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    	//GLES20.glEnable(GLES20.GL_CULL_FACE);

    	m_arrow = new Arrow();
    	m_arrow.load(m_glView);
    	m_arrow.scaleUniform(0.2f);
    	translateCamera(0,1.0f,0);
    }
    
    public void translateCamera(float x, float y, float z) {
    	xTrans += x;
    	yTrans += y;
    	zTrans += z;
    }
}
